<?php

namespace app\models;

use Yii;
use yii\data\ArrayDataProvider;

/**
 * ModelSearch represents the model behind the search form about `app\models\Model`.
 */
class ModelSearch extends Model
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'category_id', 'version', 'author_id'], 'integer'],
            [['location', 'name', 'type', 'img_url', 'create_date', 'update_date', 'comment'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ArrayDataProvider
     */
    public function search($params)
    {
        $userId = Yii::$app->user->getId();
        $fbxQuery = Fbx::find()->where(['author_id' => $userId]);
        $data = array();
        foreach ($fbxQuery->each() as $model) {
            array_push($data, array('fbx' => $model->name,  'img_url' => $model->img_url));
        }
        $provider = new ArrayDataProvider([
            'allModels' => $data,
            'key' => 'img_url',
        ]);
        $this->load($params);
        if ($this->validate()) {
            return $provider;
        }
        return $provider;
    }
}
